class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
    cnt, num_a, num_b = 1, 0, 0
    while l1:
        num_a += l1.val * cnt
        l1, cnt = l1.next, cnt * 10
    cnt = 1
    while l2:
        num_b += l2.val * cnt
        l2, cnt = l2.next, cnt * 10
    total = num_a + num_b
    if total == 0:
        return ListNode(0)
    res = head = ListNode(-1)
    while total:
        head.next = ListNode(total % 10)
        head = head.next
        total //= 10
    return res.next


ln1 = ListNode(1)
ln1.next = ListNode(9)
ln2 = ListNode(1)
ln2.next = ListNode(8)
ln = addTwoNumbers(ln1, ln2)
while ln:
    print(ln.val)
    ln = ln.next
